﻿$PublishingSitesAndAllPages = "Publishing Sites and All Pages";
$PublishingSitesOnly = "Publishing Sites Only";
$RootSitesOnly = "Root Sites Only";

$label_AR_Title="ar-sa";
$label_AR_Description="Arabic Language Pack";
$label_AR_Name="Arabic Egypt";
$label_AR_Language="ar-SA";
$label_AR_Locale=1025;
$label_AR_HierarchyCreationMode=$PublishingSitesAndAllPages;
$label_AR_IsSource=$true;
$label_AR_sitecolurl = "/";
$label_AR_SiteTemplate ="BLANKINTERNET#0";
                  
$label_EN_Title="en-us";
$label_EN_Description="English Language Pack";
$label_EN_Name="English US";
$label_EN_Language="en-US";
$label_EN_Locale=1033;
$label_EN_HierarchyCreationMode=$PublishingSitesAndAllPages;
$label_EN_IsSource=$false;


function ConfigureVariationsSettings
{
   param($rootWeb);
      
      $site = Get-SPSite http://sp2013dev:30204/sites/Home3

    $guid = [Guid]$rootWeb.GetProperty("_VarRelationshipsListId");
      $list = $rootWeb.Lists[$guid];
    $rootFolder = $list.RootFolder;
    $rootFolder.Properties["EnableAutoSpawnPropertyName"] ="false";
    $list.RootFolder.Properties["AutoSpawnStopAfterDeletePropertyName"] = "false";
    $list.RootFolder.Properties["UpdateWebPartsPropertyName"] = "false";
    $list.RootFolder.Properties["CopyResourcesPropertyName"] = "true";
    $list.RootFolder.Properties["SendNotificationEmailPropertyName"] = "false";
    $list.RootFolder.Properties["SourceVarRootWebTemplatePropertyName"] = "CMSPUBLISHING#0";
    $list.RootFolder.Update();
    $item = $null;
    if (($list.Items.Count -gt 0))
    {
       $item = $list.Items[0];
    }
    else
    {
        $item = $list.Items.Add();
        $item["GroupGuid"] = new-object System.Guid("F68A02C8-2DCC-4894-B67D-BBAED5A066F9");
    }

    $item["Deleted"] = $false;
    $item["ObjectID"] = $site.RootWeb.ServerRelativeUrl;
    $item["ParentAreaID"] = [System.String]::Empty;
    $item.Update();
}

function CreateVariations
{
    param($rootWeb);

    $guid = [Guid]$rootWeb.GetProperty("_VarLabelsListId");
    $list = $rootWeb.Lists[$guid];
    $item = $list.Items.Add();
    $item["Title"] = $label_AR_Title;
    $item["Description"] = $label_AR_Description;
    $item["Flag Control Display Name"] = $label_AR_FlagControlDisplayName;
    $item["Language"] = $label_AR_Language;
    $item["Locale"] = $label_AR_Locale;
    $item["Hierarchy Creation Mode"] = $label_AR_HierarchyCreationMode;
    $item["Is Source"] = $label_AR_IsSource;
    $item["Hierarchy Is Created"] = $false;
      $item.Update();

     $item = $list.Items.Add();
    $item["Title"] = $label_EN_Title;
    $item["Description"] = $label_EN_Description;
    $item["Flag Control Display Name"] = $label_EN_FlagControlDisplayName;
    $item["Language"] = $label_EN_Language;
    $item["Locale"] = $label_EN_Locale;
    $item["Hierarchy Creation Mode"] = $label_EN_HierarchyCreationMode;
    $item["Is Source"] = $label_EN_IsSource;
    $item["Hierarchy Is Created"] = $false;
      $item.Update();
}


function CreateHierarchies
{
    #param($site, $rootWeb);
      
      $site = Get-SPSite http://sp2013dev:30204/sites/Home3
      $rootWeb = Get-SPWeb http://sp2013dev:30204/sites/Home3
      
      $id = [Guid]("e7496be8-22a8-45bf-843a-d1bd83aceb25");
    $site.AddWorkItem([System.Guid]::Empty, [System.DateTime]::Now.ToUniversalTime(), $id, $rootWeb.ID, $site.ID, 1, $false, [System.Guid]::Empty, [System.Guid]::Empty, $rootWeb.CurrentUser.ID, $null, [System.String]::Empty, [System.Guid]::Empty, $false);
      
    $webApplication = $site.WebApplication;
    $variationsJob = $webApplication.JobDefinitions | where { $_.Name -match "VariationsCreateHierarchies" };
      
      $variationsJob.RunNow();
}

function ClearVariations 
{
      $site = Get-SPSite http://sp2013dev:30204/sites/Home3
      $varListId = New-Object -TypeName GUID -ArgumentList $site.RootWeb.AllProperties["_VarLabelsListId"];
      $varList = $site.RootWeb.Lists[$varListId];
      
      while($varList.Items.Count -ne 0){
      try{
            $varsite = Get-SPWeb "$($site.Url)/$(varList.Items[0].Name)" SilentlyContinue
      }
      catch {}
      $varList.Items.Delete(0);
      $varList.Update();
      }
}

Clear-Host
Add-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue
      
      $site = Get-SPSite http://sp2013dev:30204/sites/Home3
      $rootWeb = Get-SPWeb http://sp2013dev:30204/sites/Home3
      
      #ClearVariations;
      
      Write-Host "Configuring Variation Settings..."
      ConfigureVariationsSettings($rootWeb);
      
      Write-Host "Create Variations..."
   CreateVariations($rootWeb);
      
      Write-Host "Create Hierarchies..."
    CreateHierarchies;
